package com.nit.mapper;

import com.nit.dto.UserDTO;
import com.nit.entity.UserEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

       @Select({"SELECT " +
            "user_id, user_name, user_pwd, user_phone, role_id " +
            "FROM tb_user " +
            "WHERE is_delete = 0 " +
            "AND user_name = #{userName} " +
            "AND user_pwd = #{userPwd} "})
    UserDTO loginCheck(@Param("userName") String userName,
                       @Param("userPwd") String userPwd);

    @Select("SELECT COUNT(*) " +
            "FROM tb_user " +
            "WHERE user_name = #{userName} ")
    Integer userIsExist(@Param("userName") String userName);

    @Insert("INSERT INTO " +
            "tb_user (user_name, user_pwd, user_phone, role_id, gmt_create, gmt_modified ) " +
            "VALUES (#{userEntity.userName}, #{userEntity.userPwd}, #{userEntity.userPhone}, " +
            "#{userEntity.roleId}, #{userEntity.gmtCreate}, #{userEntity.gmtModified}) ")
    @Options(useGeneratedKeys = true, keyColumn = "user_id", keyProperty = "userId")
    Integer register(@Param("userEntity") UserEntity userEntity);

    @Update("UPDATE tb_user " +
            "SET is_delete = 1 " +
            "WHERE user_id = #{userId} ")
    Integer remove(@Param("userId") Long userId);

    @Update("UPDATE tb_user " +
            "SET " +
            "user_name = #{userDTO.userName}, user_phone = #{userDTO.userPhone}, " +
            "user_image = #{userDTO.userImage}, role_id = #{userDTO.roleId} " +
            "WHERE user_id = #{userDTO.userId} ")
    Integer update(@Param("userDTO") UserDTO userDTO);

    @Update("UPDATE tb_user " +
            "SET is_enable = 1 - is_enable " +
            "WHERE user_id = #{userId} ")
    Integer change(@Param("userId") Long userId);

    @Select("SELECT user_id, user_name,user_phone, is_enable, gmt_create " +
            "FROM tb_user " +
            "WHERE " +
            "user_name LIKE CONCAT('%', #{userName}, '%') " +
            "AND is_delete = 0 ")
    List<UserDTO> query(@Param("userName") String userName);
}
